package com.horoscope.astrology.zodiac.signs.free.activity

import android.graphics.Typeface
import android.os.Bundle
import android.view.View
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.ImageView
import android.widget.TextView
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.utils.*
import kotlinx.android.synthetic.main.item_share.*

class FortuneActivity : BaseActivity() {

    //    private lateinit var fadeOut: Animation
    private var once = true
    private lateinit var mOk: TextView
    private lateinit var mImage: ImageView
    private lateinit var mTip: TextView
    private lateinit var mResult: TextView
    private lateinit var fadeIn: Animation
    private var timeAD = 0


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_fortune)
        initTitleBar(
            this,
            R.id.btn_back_edit_name,
            R.id.text_view_back_title,
            getString(R.string.label_fortune_cookies)
        )
        activityShowAd("Interstitial_Fortune")
        mOk = findViewById(R.id.text_view_ok_magic)
        mImage = findViewById(R.id.image_view_ball)
        mResult = findViewById(R.id.text_view_cookies)
        mTip = findViewById(R.id.text_view_magic)
        fadeIn = AnimationUtils.loadAnimation(this, R.anim.fade_in)
        mOk.setOnClickListener {
            if (once) {
                timeAD++
                if (timeAD > 5) {
                    timeAD = 0
                    activityShowAd("Interstitial_Fortune")
                }
                // Fortune cookis 页面点击了 continue
                FlurryEventUtil.putDataToEvent(EventType.TYPE_FORTUNE_COOKIE_CONTINUE, null, null)
                mTip.clearAnimation()
                mTip.visibility = View.VISIBLE

                startAnim()
                once = false
            }
        }


        fadeIn.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationRepeat(animation: Animation?) {
            }

            override fun onAnimationEnd(animation: Animation?) {
                once = true
                mOk.text = getString(R.string.label_try_again)
                mOk.startAnimation(
                    AnimationUtils.loadAnimation(
                        this@FortuneActivity,
                        R.anim.fade_in
                    )
                )
            }

            override fun onAnimationStart(animation: Animation?) {
            }

        })
        // Fortune cookis 页面显示出来
        FlurryEventUtil.putDataToEvent(EventType.TYPE_FORTUNE_COOKIE_SHOW, null, null)
        ShareManager.initShare(
            this@FortuneActivity,
            null,
            null,
            null,
            image_view_more_share,
            tv_facebook
        )
    }

//    private fun setMargin() {
//        val h = SysUtils.getScreenHeightPixels(this)
//        val h1 = SysUtils.dp2sp(h, this)
//        val p = mOk.layoutParams as ConstraintLayout.LayoutParams
//        p.topMargin = (h1 / 11).toInt()
//        p.leftMargin = SysUtils.rp(40f)
//        p.rightMargin = SysUtils.rp(40f)
//        mOk.layoutParams = p
//    }

    private fun startAnim() {
        mResult.text = ""
        mOk.startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_out))
        FrameAnimationUtils.startFortune(mImage)
        var duration = 0
        for (i in 0 until 90) {
            duration += 30
        }

        mOk.postDelayed({
            val typeface = Typeface.createFromAsset(assets, "Damion-Regular.ttf")
            mResult.typeface = typeface
            mResult.text = ConstellationUtil.getFortuneTip()
            mResult.startAnimation(fadeIn)
            val anim = AnimationUtils.loadAnimation(this, R.anim.fade_out)
            mTip.startAnimation(anim)
            anim.setAnimationListener(object : Animation.AnimationListener {
                override fun onAnimationRepeat(animation: Animation?) {
                }

                override fun onAnimationEnd(animation: Animation?) {
                    //mTip2.text = ConstellationUtil.getFortuneTip()
                    //mTip2.startAnimation(fadeIn)
                }

                override fun onAnimationStart(animation: Animation?) {
                }

            })
        }, duration.toLong())

    }

    override fun onDestroy() {
        super.onDestroy()
        if (AnimationManager.getInstance().getmDisposable() != null) {
            AnimationManager.getInstance().getmDisposable().dispose()
        }
        if (AnimationManager.getInstance().subscribe != null) {
            AnimationManager.getInstance().subscribe.dispose()
        }

    }
}